package org.shj.thread.basic;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

import org.shj.thread.util.Util;

public class PrintThread {

	public static void main(String[] args){
		for(int i = 0 ; i < 5; i++){
			String s = "In Thread " + i;
			new Thread(new Runnable(){
				public void run(){
					System.out.println(s);
					Util.sleep(2);
				}
			}, "Thread" + i).start();
		}
		
		//获取java线程管理MXBean
		ThreadMXBean bean = ManagementFactory.getThreadMXBean();
		
		//不需要获取同步的monitor和sysnchronizer信息，仅获取线程和线程堆栈信息
		ThreadInfo[] dumpAllThreads = bean.dumpAllThreads(false, false);
		
		for(ThreadInfo info : dumpAllThreads){
			System.out.println("[" + info.getThreadId() + "] " + info.getThreadName());
		}
	}
}
